package com.changfen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.changfen.domain.Order;
import com.changfen.dto.OrderDto;
import com.changfen.service.OrderService;
import com.changfen.service.TccOrderService;
import com.fasterxml.jackson.databind.JsonNode;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author changfeng.chen@things-matrix.com
 * @date 2022/9/28 18:21
 **/
@Slf4j
@Service
public class TccOrderServiceImpl implements TccOrderService {
    @Resource
    private OrderService orderService;

    @Override
    public boolean createAddOrder(BusinessActionContext businessActionContext, Order order) {
        log.info("createAddOrder commit sucess order");
        return true;
    }

    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        JSONObject jsonObject = (JSONObject)businessActionContext.getActionContext("order");
        Order order = orderService.getOne(jsonObject.getLong("id"));
        order.setStatus(1);
        orderService.saveAndFlush(order);
        log.info("createAddOrder commit sucess order = {}", jsonObject);
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext businessActionContext) {
        JSONObject jsonObject = (JSONObject)businessActionContext.getActionContext("order");
        Order order = orderService.getOne(jsonObject.getLong("id"));
        order.setStatus(2);
        orderService.saveAndFlush(order);
        log.info("createAddOrder rollback sucess order = {}", jsonObject);
        return true;
    }
}
